home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / char_traits.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  4.0 KB  |  153 lines

  1. /*
  2.  * Copyright (c) 1997
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Silicon Graphics makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  */ 
  13.  
  14. #ifndef __SGI_STL_CHAR_TRAITS_H
  15. #define __SGI_STL_CHAR_TRAITS_H
  16.  
  17. #include <string.h>
  18. #include <wchar.h>
  19.  
  20. #if defined(__STL_USE_NEW_IOSTREAMS) && !defined(__SGI_STL_IOSFWD)
  21. #include <iosfwd>
  22. #endif /* use new iostreams */
  23.  
  24. __STL_BEGIN_NAMESPACE
  25.  
  26. // Class __char_traits_base.
  27.  
  28. template <class _CharT, class _IntT> class __char_traits_base {
  29. public:
  30.   typedef _CharT char_type;
  31.   typedef _IntT int_type;
  32. #ifdef __STL_USE_NEW_IOSTREAMS
  33.   typedef streamoff off_type;
  34.   typedef streampos pos_type;
  35.   typedef mbstate_t state_type;
  36. #endif /* __STL_USE_NEW_IOSTREAMS */
  37.  
  38.   static void assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
  39.   static bool eq(const _CharT& __c1, const _CharT& __c2) 
  40.     { return __c1 == __c2; }
  41.   static bool lt(const _CharT& __c1, const _CharT& __c2) 
  42.     { return __c1 < __c2; }
  43.  
  44.   static int compare(const _CharT* __s1, const _CharT* __s2, size_t __n) {
  45.     for (size_t __i = 0; __i < __n; ++__i)
  46.       if (!eq(__s1[__i], __s2[__i]))
  47.         return __s1[__i] < __s2[__i] ? -1 : 1;
  48.     return 0;
  49.   }
  50.  
  51.   static size_t length(const _CharT* __s) {
  52.     const _CharT __nullchar = _CharT();
  53.     size_t __i;
  54.     for (__i = 0; !eq(__s[__i], __nullchar); ++__i)
  55.       {}
  56.     return __i;
  57.   }
  58.  
  59.   static const _CharT* find(const _CharT* __s, size_t __n, const _CharT& __c)
  60.   {
  61.     for ( ; __n > 0 ; ++__s, --__n)
  62.       if (eq(*__s, __c))
  63.         return __s;
  64.     return 0;
  65.   }
  66.  
  67.   static _CharT* move(_CharT* __s1, const _CharT* __s2, size_t __n) {
  68.     memmove(__s1, __s2, __n * sizeof(_CharT));
  69.     return __s1;
  70.   }
  71.     
  72.   static _CharT* copy(_CharT* __s1, const _CharT* __s2, size_t __n) {
  73.     memcpy(__s1, __s2, __n * sizeof(_CharT));
  74.     return __s1;
  75.   } 
  76.  
  77.   static _CharT* assign(_CharT* __s, size_t __n, _CharT __c) {
  78.     for (size_t __i = 0; __i < __n; ++__i)
  79.       __s[__i] = __c;
  80.     return __s;
  81.   }
  82.  
  83.   static int_type not_eof(const int_type& __c) {
  84.     return !eq_int_type(__c, eof()) ? __c : 0;
  85.   }
  86.  
  87.   static char_type to_char_type(const int_type& __c) {
  88.     return static_cast<char_type>(__c);
  89.   }
  90.  
  91.   static int_type to_int_type(const char_type& __c) {
  92.     return static_cast<int_type>(__c);
  93.   }
  94.  
  95.   static bool eq_int_type(const int_type& __c1, const int_type& __c2) {
  96.     return __c1 == __c2;
  97.   }
  98.  
  99.   static int_type eof() {
  100.     return static_cast<int_type>(-1);
  101.   }
  102. };
  103.  
  104. // Generic char_traits class.  Note that this class is provided only
  105. //  as a base for explicit specialization; it is unlikely to be useful
  106. //  as is for any particular user-defined type.  In particular, it 
  107. //  *will not work* for a non-POD type.
  108.  
  109. template <class _CharT> class char_traits
  110.   : public __char_traits_base<_CharT, _CharT>
  111. {};
  112.  
  113. // Specialization for char.
  114.  
  115. __STL_TEMPLATE_NULL class char_traits<char> 
  116.   : public __char_traits_base<char, int>
  117. {
  118. public:
  119.   static char_type to_char_type(const int_type& __c) {
  120.     return static_cast<char_type>(static_cast<unsigned char>(__c));
  121.   }
  122.  
  123.   static int_type to_int_type(const char_type& __c) {
  124.     return static_cast<unsigned char>(__c);
  125.   }
  126.  
  127.   static int compare(const char* __s1, const char* __s2, size_t __n) 
  128.     { return memcmp(__s1, __s2, __n); }
  129.   
  130.   static size_t length(const char* __s) { return strlen(__s); }
  131.  
  132.   static void assign(char& __c1, const char& __c2) { __c1 = __c2; }
  133.  
  134.   static char* assign(char* __s, size_t __n, char __c)
  135.     { memset(__s, __c, __n); return __s; }
  136. };
  137.  
  138. // Specialization for wchar_t.
  139.  
  140. __STL_TEMPLATE_NULL class char_traits<wchar_t>
  141.   : public __char_traits_base<wchar_t, wint_t>
  142. {};
  143.  
  144.  
  145. __STL_END_NAMESPACE
  146.  
  147. #endif /* __SGI_STL_CHAR_TRAITS_H */
  148.  
  149. // Local Variables:
  150. // mode:C++
  151. // End:
  152.  
  153.